﻿// MedicalRulesEngine.Core/Services/IRuleService.cs
using MedicalRulesEngine.Core.DTOs;
using System.Linq.Dynamic.Core;

namespace MedicalRulesEngine.Core.Services;

/// <summary>
/// 规则服务接口
/// 提供规则的业务逻辑操作
/// </summary>
public interface IRuleService
{
    /// <summary>
    /// 获取所有规则
    /// </summary>
    /// <returns>规则集合</returns>
    Task<IEnumerable<RuleDto>> GetAllRulesAsync();

    /// <summary>
    /// 根据ID获取规则
    /// </summary>
    /// <param name="id">规则ID</param>
    /// <returns>规则对象</returns>
    Task<RuleDto?> GetRuleByIdAsync(string id);

    /// <summary>
    /// 根据代码获取规则
    /// </summary>
    /// <param name="code">规则代码</param>
    /// <returns>规则对象</returns>
    Task<RuleDto?> GetRuleByCodeAsync(string code);

    /// <summary>
    /// 根据工作流获取规则
    /// </summary>
    /// <param name="workflowName">工作流名称</param>
    /// <returns>规则集合</returns>
    Task<IEnumerable<RuleDto>> GetRulesByWorkflowAsync(string workflowName);

    /// <summary>
    /// 根据分类获取规则
    /// </summary>
    /// <param name="category">分类</param>
    /// <returns>规则集合</returns>
    Task<IEnumerable<RuleDto>> GetRulesByCategoryAsync(string category);

    /// <summary>
    /// 搜索规则
    /// </summary>
    /// <param name="searchTerm">搜索词</param>
    /// <param name="category">分类过滤</param>
    /// <param name="isEnabled">启用状态过滤</param>
    /// <returns>符合条件的规则集合</returns>
    Task<IEnumerable<RuleDto>> SearchRulesAsync(string? searchTerm = null,
        string? category = null, bool? isEnabled = null);

    /// <summary>
    /// 分页获取规则
    /// </summary>
    /// <param name="pageIndex">页索引</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="searchTerm">搜索词</param>
    /// <param name="category">分类过滤</param>
    /// <param name="isEnabled">启用状态过滤</param>
    /// <returns>分页结果</returns>
    Task<PagedResult<RuleDto>> GetRulesPagedAsync(int pageIndex, int pageSize,
        string? searchTerm = null, string? category = null, bool? isEnabled = null);

    /// <summary>
    /// 创建规则
    /// </summary>
    /// <param name="request">创建规则请求</param>
    /// <returns>创建的规则</returns>
    Task<RuleDto> CreateRuleAsync(CreateRuleRequest request);

    /// <summary>
    /// 更新规则
    /// </summary>
    /// <param name="id">规则ID</param>
    /// <param name="request">更新规则请求</param>
    /// <returns>更新后的规则</returns>
    Task<RuleDto> UpdateRuleAsync(string id, UpdateRuleRequest request);

    /// <summary>
    /// 删除规则
    /// </summary>
    /// <param name="id">规则ID</param>
    Task DeleteRuleAsync(string id);

    /// <summary>
    /// 启用规则
    /// </summary>
    /// <param name="id">规则ID</param>
    Task EnableRuleAsync(string id);

    /// <summary>
    /// 禁用规则
    /// </summary>
    /// <param name="id">规则ID</param>
    Task DisableRuleAsync(string id);

    /// <summary>
    /// 复制规则
    /// </summary>
    /// <param name="sourceRuleId">源规则ID</param>
    /// <param name="newName">新规则名称</param>
    /// <param name="newCode">新规则代码</param>
    /// <returns>复制后的规则</returns>
    Task<RuleDto> CloneRuleAsync(string sourceRuleId, string newName, string newCode);

    /// <summary>
    /// 验证规则
    /// </summary>
    /// <param name="rule">规则对象</param>
    /// <returns>验证结果</returns>
    Task<ValidationResult> ValidateRuleAsync(RuleDto rule);

    /// <summary>
    /// 执行规则
    /// </summary>
    /// <param name="ruleId">规则ID</param>
    /// <param name="inputData">输入数据</param>
    /// <returns>执行结果</returns>
    Task<RuleExecutionResult> ExecuteRuleAsync(string ruleId, object inputData);

    /// <summary>
    /// 批量执行规则
    /// </summary>
    /// <param name="workflowName">工作流名称</param>
    /// <param name="inputData">输入数据</param>
    /// <returns>执行结果集合</returns>
    Task<IEnumerable<RuleExecutionResult>> ExecuteWorkflowRulesAsync(string workflowName, object inputData);
}

/// <summary>
/// 验证结果
/// </summary>
public class ValidationResult
{
    /// <summary>
    /// 是否有效
    /// </summary>
    public bool IsValid { get; set; }

    /// <summary>
    /// 错误消息集合
    /// </summary>
    public List<string> Errors { get; set; } = new();

    /// <summary>
    /// 警告消息集合
    /// </summary>
    public List<string> Warnings { get; set; } = new();
}

/// <summary>
/// 规则执行结果
/// </summary>
public class RuleExecutionResult
{
    /// <summary>
    /// 规则ID
    /// </summary>
    public string RuleId { get; set; } = string.Empty;

    /// <summary>
    /// 规则名称
    /// </summary>
    public string RuleName { get; set; } = string.Empty;

    /// <summary>
    /// 是否执行成功
    /// </summary>
    public bool IsSuccess { get; set; }

    /// <summary>
    /// 执行结果
    /// </summary>
    public bool Result { get; set; }

    /// <summary>
    /// 执行耗时（毫秒）
    /// </summary>
    public long ExecutionTimeMs { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 执行的动作集合
    /// </summary>
    public List<ActionExecutionResult> ActionResults { get; set; } = new();

    /// <summary>
    /// 执行上下文
    /// </summary>
    public Dictionary<string, object> Context { get; set; } = new();
}

/// <summary>
/// 动作执行结果
/// </summary>
public class ActionExecutionResult
{
    /// <summary>
    /// 动作名称
    /// </summary>
    public string ActionName { get; set; } = string.Empty;

    /// <summary>
    /// 动作类型
    /// </summary>
    public string ActionType { get; set; } = string.Empty;

    /// <summary>
    /// 是否执行成功
    /// </summary>
    public bool IsSuccess { get; set; }

    /// <summary>
    /// 执行结果
    /// </summary>
    public object? Result { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? ErrorMessage { get; set; }
}
